package com.hzx.controller;

import com.hzx.pojo.Sort;
import com.hzx.service.ItemService;
import com.hzx.service.KindService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class GridShopController {

    @Autowired
    ItemService itemService ;

    @Autowired
    KindService kindService ;

    @RequestMapping(value = "/selective",method = RequestMethod.GET)
    @ResponseBody
    public List select(@RequestParam String low , @RequestParam String heigh,@RequestParam String kindlist){
        Integer lo = Integer.parseInt(low) ;
        Integer he = Integer.parseInt(heigh) ;
        List items = new ArrayList() ;
        for (int i=0;i<kindlist.length();i++){
            char chr = kindlist.charAt(i) ;
            if(chr==' '){
                continue;
            }
            Integer kid = chr-'0' ;
            items.addAll(itemService.selectByKidAndPrice(kid,lo,he)) ;
        }
        return items ;
    }

    @GetMapping("/grid-shop.html")
    public String gridshop(Model model){
        List itemList = itemService.getAllItems();
        List kindList = kindService.getAllKind();
        Map kindNumbMap = getItemsNumber() ;
        model.addAttribute("items",itemList) ;
        model.addAttribute("kindList",kindList) ;
        model.addAttribute("numbMap",kindNumbMap) ;
        return "grid-shop" ;
    }

    //因为现在我们的库存不多，所以一次把所有产品都取出来
    private List getRecommand() {
        return null ;
    }
    //获取各种类别的商品的数量
    public Map getItemsNumber(){
        Map<Integer,Integer> kindsNumb = new HashMap();
        List kindlist = kindService.getAllKind() ;
        int sum = 0 ;
        for (Object o : kindlist) {
            Sort sort = (Sort)o ;
            Integer numb = itemService.getNumbsByKindId(sort.getId()) ;
            sum+=numb ;
            kindsNumb.put(sort.getId(),numb) ;
        }
        kindsNumb.put(0,sum) ;
        return kindsNumb ;
    }

    private Map getKindMapper() {
        Map map = new HashMap<Integer,String>() ;
        List list = kindService.getAllKind() ;
        for (Object o : list) {
            Sort sort =(Sort) o ;
            map.put(sort.getId(),sort.getKind()) ;
        }
        return map ;
    }

    //通过条件来筛选商品
    //ajax
    @RequestMapping("")
    @ResponseBody
    public List selectiveItem(@RequestParam int upper,@RequestParam int lower,@RequestParam List kindMapper){
        List item = null ;
        return item;
    }
}
